#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
    /*打开文件*/
    const char * filename = "./record.txt";
    int fd = open(filename,O_RDWR | O_CREAT,0644);
    if(fd == -1)
    {
        perror("open error:");
        exit(-1);
    }
    printf("fd = %d\n",fd);

    /* 从文件开头向后移动20字节 */
    off_t offset = lseek(fd,20,SEEK_SET);
    printf("offset = %ld\n",offset);

    char buffer[128] = {0};
    read(fd,buffer,sizeof(buffer) - 1);
    printf("buffer = %s\n",buffer);

    /* 可通过lseek函数计算得到文件的大小 */
    offset =  lseek(fd,0,SEEK_END);
    printf("offset = %ld\n",offset);
    

    return 0;
}